package com.wisewin.circle.util;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.schedule.ScheduleResult;

import java.util.HashMap;
import java.util.Map;

public class JPushUtil {

    private static String APP_KEY = "94bbe001d6153e1a147036cc";
    private static String MASTER_SECRET = "5a66cae61bb8f70189cf606c";
    private static long TIME_TO_LIVE = 86400;//设置有效期一天 60*60*24

    // 推送给所有用户
    public static void jpushAll(Map<String, String> param, Map<String, String> extras, boolean apnsProduction) {
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        //ios需要单独设置标题
        IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody(param.get("title"),"",param.get("msg")).build();
        //android需要设置推送优先走极光，极光不在线再走厂商，厂商作为辅助
        Map<String,Map<String,String>> thirdPartyChannel = new HashMap<>();
        Map<String,String> map = new HashMap<>();
        map.put("distribution","secondary_push");
        thirdPartyChannel.put("xiaomi",map);
        thirdPartyChannel.put("huawei",map);
        thirdPartyChannel.put("oppo",map);
        thirdPartyChannel.put("vivo",map);
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all()) // 所有平台的用户
                .setAudience(Audience.all()) // 项目中的所有用户
                .setNotification(Notification.newBuilder()
                        // 发送ios
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(iosAlert)// 消息体
                                .autoBadge()//app角标
                                .setSound("default")// ios提示音
                                .addExtras(extras)// 附加参数
                                .build())
                        // 发送android
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle(param.get("title"))
                                .addExtras(extras)// 附加参数
                                .setAlert(param.get("msg"))// 消息体
                                .addCustom("uri_activity", ".ui.MainActivity")
                                .addCustom("uri_action", ".ui.MainActivity")
                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(apnsProduction).setTimeToLive(TIME_TO_LIVE).setThirdPartyChannel(thirdPartyChannel).build())
                .setMessage(Message.content(param.get("msg")))// 自定义信息
                .build();
        try {
            if("atOnce".equals(param.get("triggerTime"))){
                PushResult pu = jpushClient.sendPush(payload);
                System.out.println("======返回结果====="+pu.toString());
            }else{
                ScheduleResult result = jpushClient.createSingleSchedule(param.get("title"), param.get("triggerTime"), payload);
                System.out.println("======返回结果====="+result.toString());
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

    // 根据标签推送
    public static void jpushByTag(Map<String, String> param, Map<String, String> extras, boolean apnsProduction) {
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        //ios需要单独设置标题
        IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody(param.get("title"),"",param.get("msg")).build();
        //android需要设置推送优先走极光，极光不在线再走厂商，厂商作为辅助
        Map<String,Map<String,String>> thirdPartyChannel = new HashMap<>();
        Map<String,String> map = new HashMap<>();
        map.put("distribution","secondary_push");
        thirdPartyChannel.put("xiaomi",map);
        thirdPartyChannel.put("huawei",map);
        thirdPartyChannel.put("oppo",map);
        thirdPartyChannel.put("vivo",map);
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())// 所有平台的用户
                .setAudience(Audience.tag(param.get("tag"))) // 设置按标签发送，相当于群发
                .setNotification(Notification.newBuilder()
                        // 发送ios
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(iosAlert)// 消息体
                                .autoBadge()//app角标
                                .setSound("default")// ios提示音
                                .addExtras(extras)// 附加参数
                                .build())
                        // 发送android
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle(param.get("title"))
                                .addExtras(extras)// 附加参数
                                .setAlert(param.get("msg"))// 消息体
                                .addCustom("uri_activity", ".ui.MainActivity")
                                .addCustom("uri_action", ".ui.MainActivity")
                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(apnsProduction).setTimeToLive(TIME_TO_LIVE).setThirdPartyChannel(thirdPartyChannel).build())
                .setMessage(Message.content(param.get("msg")))// 自定义信息
                .build();
        try {
            if("atOnce".equals(param.get("triggerTime"))){
                PushResult pu = jpushClient.sendPush(payload);
                System.out.println("======返回结果====="+pu.toString());
            }else{
                ScheduleResult result = jpushClient.createSingleSchedule(param.get("title"), param.get("triggerTime"), payload);
                System.out.println("======返回结果====="+result.toString());
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

    // 根据别名推送
    public static void jpushByAlias(Map<String, String> param, Map<String, String> extras, boolean apnsProduction) {
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        //ios需要单独设置标题
        IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody(param.get("title"),"",param.get("msg")).build();
        //android需要设置推送优先走极光，极光不在线再走厂商，厂商作为辅助
        Map<String,Map<String,String>> thirdPartyChannel = new HashMap<>();
        Map<String,String> map = new HashMap<>();
        map.put("distribution","secondary_push");
        thirdPartyChannel.put("xiaomi",map);
        thirdPartyChannel.put("huawei",map);
        thirdPartyChannel.put("oppo",map);
        thirdPartyChannel.put("vivo",map);
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())// 所有平台的用户
                .setAudience(Audience.alias(param.get("alias")))
                .setNotification(Notification.newBuilder()
                        // 发送ios
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(iosAlert)// 消息体
                                .autoBadge()//app角标
                                .setSound("default")// ios提示音
                                .addExtras(extras)// 附加参数
                                .build())
                        // 发送android
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle(param.get("title"))
                                .addExtras(extras)// 附加参数
                                .setAlert(param.get("msg"))// 消息体
                                .addCustom("uri_activity", ".ui.MainActivity")
                                .addCustom("uri_action", ".ui.MainActivity")
                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(apnsProduction).setTimeToLive(TIME_TO_LIVE).setThirdPartyChannel(thirdPartyChannel).build())
                .setMessage(Message.content(param.get("msg")))// 自定义信息
                .build();
        try {
            if("atOnce".equals(param.get("triggerTime"))){
                PushResult pu = jpushClient.sendPush(payload);
                System.out.println("======返回结果====="+pu.toString());
            }else{
                ScheduleResult result = jpushClient.createSingleSchedule(param.get("title"), param.get("triggerTime"), payload);
                System.out.println("======返回结果====="+result.toString());
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

    /**
     *@Description: 系统后台调用此方法进行推送
     *@author: wsx
     *@date: 2021/5/25 9:21
     */
    public static void jpush(Map<String, String> param, Map<String, String> extras, boolean apnsProduction){
        if("all".equals(param.get("pushType"))){
            jpushAll(param,extras,apnsProduction);
        }else if("tag".equals(param.get("pushType"))){
            jpushByTag(param,extras,apnsProduction);
        }else if("alias".equals(param.get("pushType"))){
            jpushByAlias(param,extras,apnsProduction);
        }
    }

    public static void main(String[] args) {
        Map<String, String> param = new HashMap<String, String>();
        param.put("pushType","alias");
        param.put("triggerTime","atOnce");
        param.put("title","灵笼更新终章");
        param.put("msg","红蔻复活，冉冰复活。");
        param.put("tag","user");
        param.put("alias","13333333333");
        Map<String, String> extras = new HashMap<String, String>();
        extras.put("jumpType","courseDetails");
        extras.put("resource","270");
        boolean apnsProduction = false;
        jpush(param,extras,apnsProduction);
    }

}
